package com.sneakxy.cloudbase.platform.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.sneakxy.cloudbase.platform.bean.Dict;
import com.sneakxy.cloudbase.platform.bean.dto.DictDTO;
import com.sneakxy.cloudbase.platform.repository.DictRepository;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service
public class DictService extends BaseService<Dict, String, DictRepository> implements IDictService {

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IDictService#findByType(java.lang.String, java.lang.String, boolean)
	 */
	@Override
	public DictDTO findByType(String parentId, String value, boolean children) {
		final CriteriaBuilder<Dict> builder = new CriteriaBuilder<>(Dict.class);
		if(StringUtils.isBlank(parentId)) {
			builder.andIsNull(DictDTO.FIELD_NAME_PARENT_ID);
		} else {
			builder.andEq(DictDTO.FIELD_NAME_PARENT_ID, parentId);
		}
		builder.andEq(DictDTO.FIELD_NAME_VALUE, value);
		Dict dict = getRepository().findOne(builder.build());
		DictDTO result = dict != null ? DictDTO.from(dict) : null;
		if(children && result != null) {
			result.setChildren(findDicts(result.getId()));
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IDictService#findDicts(java.lang.String, boolean)
	 */
	@Override
	public List<DictDTO> findDicts(String parentId) {
		final CriteriaBuilder<Dict> builder = new CriteriaBuilder<>(Dict.class);
		if(StringUtils.isBlank(parentId)) {
			builder.andIsNull(DictDTO.FIELD_NAME_PARENT_ID);
		} else {
			builder.andEq(DictDTO.FIELD_NAME_PARENT_ID, parentId);
		}
		
		final Sort sort = Sort.by(new Order(Direction.ASC, "sort"));
		return DictDTO.from(getRepository().findAll(builder.build(), sort));
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IDictService#findByValues(java.lang.String[], boolean)
	 */
	@Override
	public DictDTO findByValues(String[] values, boolean children) {
		String parentId = null;
		DictDTO dict = null;
		for(String value: values) {
			dict = findByType(parentId, value, false);
			if(dict == null) {
				break;
			}
			parentId = dict.getId();
		}
		if(children && dict != null) {
			dict.setChildren(this.findDicts(parentId));
		}
		return dict;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IDictService#hasChildren(java.lang.String)
	 */
	@Override
	public boolean hasChildren(String id) {
		CriteriaBuilder<Dict> builder = new CriteriaBuilder<>(Dict.class);
		builder.andEq(DictDTO.FIELD_NAME_PARENT_ID, id);
		return this.getRepository().count(builder.build()) > 0;
	}

	@Override
	public boolean remove(String id) {
		if(!hasChildren(id)) {
			delete(id);
			return true;
		}
		return false;
	}

}
